
import java.util.PriorityQueue;
import java.util.Timer;
import java.util.TimerTask;

class MyTimerTask implements Comparable<MyTimerTask> {
    private long time;
    private Runnable runnable;

    public MyTimerTask(Runnable runnable, long delay) {
        this.runnable = runnable;
        this.time = System.currentTimeMillis() + delay;
    }

    public long getTime() {
        return time;
    }

    public void run() {
        runnable.run();
    }

    @Override
    public int compareTo(MyTimerTask o) {
        return (int) (this.time - o.time);
    }
}

class MyTimer {
    private Object loker = new Object();
    private PriorityQueue<MyTimerTask> queue = new PriorityQueue<>();

    private Thread t = null;

    public void schedule(Runnable runnable, long delay) {
        synchronized (loker) {
            MyTimerTask task = new MyTimerTask(runnable, delay);
            queue.offer(task);
            loker.notify();
        }
    }

    MyTimer() {
        t = new Thread(() -> {
            while (true) {
                try {
                    synchronized (loker) {
                        while (queue.isEmpty()) {
                            loker.wait();
                        }
                        MyTimerTask task = queue.peek();
                        long currentTime = System.currentTimeMillis();
                        if (currentTime >= task.getTime()) {
                            queue.poll();
                            task.run();
                        } else {
                            loker.wait(task.getTime() - currentTime);
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t.start();
    }

}

public class Test {
    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("hello 3000");
            }
        },3000);

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("hello 2000");
            }
        },2000);

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("hello 1000");
            }
        },1000);

        System.out.println("hello main");
    }
    public static void main1(String[] args) {
        MyTimer timer = new MyTimer();
        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello 3000");
            }
        }, 3000);

        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello 2000");
            }
        }, 2000);

        timer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello 1000");
            }
        }, 1000);

        System.out.println("hello main");
    }
}
